import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { OUTPUT_DIR, brotliSize, chunkSizeWarningLimit, terserOptions, rollupOptions } from './build/constant'
import viteCompression from 'vite-plugin-compression'
import { axiosPre } from './src/settings/httpSetting'
import { viteMockServe } from 'vite-plugin-mock'
import monacoEditorPlugin from 'vite-plugin-monaco-editor'



// import FileManagerPlugin from 'filemanager-webpack-plugin'; // 打包生成zip文件  版本不支持
import Moment from 'moment'  // 添加时间格式

// 安装 vite 适配 版本的打包插件  npm install rollup-plugin-compression -D
import compresssionBuild from "rollup-plugin-compression";
import type { ICompressionOptions } from "rollup-plugin-compression";
// let packTimeString = Moment().format('YYYYMMDDHHmmss');// 打包时间
const option: ICompressionOptions = {
  sourceName: `dist`,
  type: "zip",
  // targetName: `dist-${packTimeString}`
  targetName: 'datav'
};

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir)
}

export default ({ mode }) =>
  defineConfig({
    base: process.env.NODE_ENV === 'production' ? './' : '/',
    // 路径重定向
    resolve: {
      alias: [
        {
          find: /\/#\//,
          replacement: pathResolve('types')
        },
        {
          find: '@',
          replacement: pathResolve('src')
        },
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js' //解决i8n警告
        },
      ],
      dedupe: ['vue']
    },
    // 全局 css 注册
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "src/styles/common/style.scss";`
        }
      }
    },
    // 开发服务器配置
    server: {
      host: true,
      open: true,
      port: 3000,
      proxy: {
        [axiosPre]: {
          // @ts-ignore
          target: loadEnv(mode, process.cwd()).VITE_DEV_PATH,
          changeOrigin: true,
          ws: true,
          secure: true
        }
      },
      hmr: {
        overlay: false
      }
    },
    plugins: [
      vue(),
      monacoEditorPlugin({
        languageWorkers: ['editorWorkerService', /*'typescript', */ 'json', 'html']
      }),
      viteMockServe({
        mockPath: '/src/api/mock',
        // 开发打包开关
        localEnabled: true,
        // 生产打包开关
        prodEnabled: true,
        // 打开后，可以读取 ts 文件模块。 请注意，打开后将无法监视.js 文件
        supportTs: true,
        // 监视文件更改
        watchFiles: true
      }),
      // 压缩
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz'
      }),
      compresssionBuild(option)
      // new FileManagerPlugin({
      //   events: {
      //     onEnd: {
      //       mkdir: ['./archive'],
      //       archive: [
      //         // 打包 ./dist 到 ./archive/dist/datetime.zip 压缩文件中，压缩包中不带dist 外壳
      //         {
      //           source: './dist',
      //           destination: `./archive/dist-${packTimeString}.zip`
      //         }
      //       ]
      //     }
      //   }
      // })
    ],
    build: {
      target: 'es2015',
      outDir: OUTPUT_DIR,
      // minify: 'terser', // 如果需要用terser混淆，可打开这两行
      // terserOptions: terserOptions,
      rollupOptions: rollupOptions,
      brotliSize: brotliSize,
      chunkSizeWarningLimit: chunkSizeWarningLimit,

      // 解决assets静态文件图片路径的问题
      assetsInlineLimit: 1
    },
    lintOnSave: false
  } as any)
